package com.lcg.word

import android.view.MotionEvent
import com.lcg.base.Loading
import com.lcg.base.showToast
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 电子墨水
 *
 * @author lei.chuguang Email:475825657@qq.com
 * @version 1.0
 * @since 2023/7/7 10:56
 */
object DigitalInkManager {

    private var inputListener: ((String) -> Unit)? = null

    /**单词转换后的a..z字符串*/
    private var compareString: String? = null
    private var compareSuccess: (() -> Unit)? = null
    private var scope = MainScope()
    private var job: Job? = null

    private fun delayRecognize() {
        job = scope.launch {
            delay(500)
            // TODO: 2023/9/28  
        }
    }

    private fun cancelRecognize() {
        job?.cancel()
        job = null
    }

    /**单词对比*/
    private fun compare(string: String, result: Any): Boolean {
        val candidates = arrayListOf<String>()// TODO: 2023/9/28  
        if (candidates.isNullOrEmpty()) return false
        //
        var success = false
        ov@ for (candidate in candidates) {
            var index = 0
            for (c in candidate) {
                if (c in 'a'..'z') {
                    if (string.getOrNull(index++) != c) {
                        continue@ov
                    }
                } else if (c in 'A'..'Z') {
                    if (string.getOrNull(index++) != c + 32) {
                        continue@ov
                    }
                }
            }
            success = index == string.length
            if (success) break@ov
        }
        return success
    }

    /**获取单词里面的a..z的字母*/
    private fun getLetter(word: String): String {
        val builder = StringBuilder()
        word.forEach {
            if (it in 'a'..'z') {
                builder.append(it)
            } else if (it in 'A'..'Z') {
                builder.append(it + 32)
            }
        }
        return builder.toString()
    }

    private var downloading = false

    /**下载模型*/
    fun download(p: Loading, success: () -> Unit) {
        if (downloading) {
            showToast("引擎正在准备中，请稍后重试")
        } else {
            val job = scope.launch {
                delay(1000)
                p.loadingMessage = "引擎初始化中..."
            }
            p.loadingMessage = "引擎初始化中..."
            p.show(job)
            downloading = true
            // TODO: 2023/9/28
            success()
            p.dismiss()
        }
    }

    fun addNewTouchEvent(event: MotionEvent) {
        cancelRecognize()
        val action = event.actionMasked
        val x = event.x
        val y = event.y
        val t = System.currentTimeMillis()
        when (action) {
            // TODO: 2023/9/28  
        }
    }

    /**手写输入监听*/
    fun setInputListener(listener: ((String) -> Unit)?) {
        inputListener = listener
    }

    /**设置比较的单词*/
    fun setCompareWord(word: String?, success: (() -> Unit)?) {
        compareString = if (word == null) null else getLetter(word)
        compareSuccess = success
    }

    /**重置*/
    fun reset() {
        cancelRecognize()
        // TODO: 2023/9/28  
    }

    /**识别*/
    fun recognize(word: String, callback: (Boolean) -> Unit) {
        // TODO: 2023/9/28  
    }
}